home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / TextAttributes.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  3.9 KB  |  138 lines

  1. package horst;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.util.Enumeration;
  6. import java.util.Hashtable;
  7.  
  8. public class TextAttributes {
  9.    static Color defaultColor;
  10.    Color color;
  11.    Color focusColor;
  12.    String href;
  13.    String target;
  14.    boolean bUnderline;
  15.    Font font;
  16.    private Hashtable atts;
  17.    private static Hashtable fontMapping;
  18.    private static int downSizeIncrement;
  19.    private static int upSizeIncrement;
  20.  
  21.    static {
  22.       defaultColor = Color.black;
  23.       fontMapping = new Hashtable();
  24.       downSizeIncrement = 2;
  25.       upSizeIncrement = 4;
  26.    }
  27.  
  28.    TextAttributes(TextAttributes copy) {
  29.       this.focusColor = Color.red;
  30.       this.bUnderline = false;
  31.       this.atts = new Hashtable();
  32.       Font copyFont = copy.font;
  33.       this.font = new Font(copyFont.getName(), copyFont.getStyle(), copyFont.getSize());
  34.       Hashtable ht = copy.atts;
  35.       Enumeration e = ht.keys();
  36.  
  37.       while(e.hasMoreElements()) {
  38.          String key = (String)e.nextElement();
  39.          String value = (String)this.atts.get(key);
  40.          if (value != null) {
  41.             this.atts.put(key, value);
  42.          } else {
  43.             this.atts.put(key, new String(""));
  44.          }
  45.       }
  46.  
  47.       this.bUnderline = copy.bUnderline;
  48.       this.color = copy.color;
  49.       this.focusColor = copy.focusColor;
  50.       this.href = copy.href;
  51.    }
  52.  
  53.    public TextAttributes(Font defFont) {
  54.       this.focusColor = Color.red;
  55.       this.bUnderline = false;
  56.       this.atts = new Hashtable();
  57.       this.font = defFont;
  58.       int defaultSize = this.font.getSize();
  59.       this.color = defaultColor;
  60.       fontMapping.put("1", new Integer(defaultSize - 2 * downSizeIncrement));
  61.       fontMapping.put("2", new Integer(defaultSize - downSizeIncrement));
  62.       fontMapping.put("3", new Integer(defaultSize));
  63.       fontMapping.put("4", new Integer(defaultSize + upSizeIncrement));
  64.       fontMapping.put("5", new Integer(defaultSize + 2 * upSizeIncrement));
  65.       fontMapping.put("6", new Integer(defaultSize + 3 * upSizeIncrement));
  66.       fontMapping.put("7", new Integer(defaultSize + 4 * upSizeIncrement));
  67.    }
  68.  
  69.    static Font createFont(Font f, String sz) {
  70.       int size = getFontSize(f.getSize(), sz);
  71.       return new Font(f.getName(), f.getStyle(), size);
  72.    }
  73.  
  74.    static int getFontSize(int origSize, String size) {
  75.       size = size.trim();
  76.       int idx;
  77.       if ((idx = size.indexOf(43)) != -1) {
  78.          String val = size.substring(idx + 1);
  79.          Integer i = Utilities.getInteger(val);
  80.          if (i != null) {
  81.             return origSize + i * upSizeIncrement;
  82.          }
  83.       } else if ((idx = size.indexOf(45)) != -1) {
  84.          Integer smallestSize = (Integer)fontMapping.get("1");
  85.          String val = size.substring(idx + 1);
  86.          Integer i = Utilities.getInteger(val);
  87.          if (i != null) {
  88.             int sz = origSize - i * downSizeIncrement;
  89.             if (sz < smallestSize) {
  90.                return smallestSize;
  91.             }
  92.  
  93.             return sz;
  94.          }
  95.       } else {
  96.          Integer val = (Integer)fontMapping.get(size);
  97.          if (val != null) {
  98.             return val;
  99.          }
  100.       }
  101.  
  102.       return origSize;
  103.    }
  104.  
  105.    void multiplexAttributes(Element elem) {
  106.       elem.setAttribute("textcolor", this.color);
  107.       elem.m_focusColor = this.focusColor;
  108.       String value = (String)elem.getAttribute("underline");
  109.       if (value == null || value.equals("false")) {
  110.          value = this.bUnderline ? "true" : "false";
  111.          elem.setAttribute("underline", value);
  112.       }
  113.  
  114.       Enumeration e = this.atts.keys();
  115.  
  116.       while(e.hasMoreElements()) {
  117.          String key = (String)e.nextElement();
  118.          value = (String)this.atts.get(key);
  119.          if (value != null && elem.getAttribute(key) == null) {
  120.             elem.setAttribute(key, value);
  121.          }
  122.       }
  123.  
  124.       if (this.href != null && this.href.length() > 0 && elem.getAttribute("href") == null) {
  125.          elem.setAttribute("href", this.href);
  126.       }
  127.  
  128.       if (this.target != null) {
  129.          elem.setAttribute("target", this.target);
  130.       }
  131.  
  132.    }
  133.  
  134.    void setAttribute(String key, Object value) {
  135.       this.atts.put(key, value);
  136.    }
  137. }
  138.